package com.baizhi.controller;

import com.baizhi.annotation.AddCacheAnnotation;
import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.Banner;
import com.baizhi.service.BannerService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;

    @LogAnnotation(value = "查询所有轮播图",type = "selectpage")
    @ResponseBody
    @RequestMapping("selectPage")
    @AddCacheAnnotation
    public Map selectPage(Integer page, Integer limit){
        Map m = new HashMap();
        PageInfo<Banner> pageInfo = bannerService.selectAll(page, limit);
        m.put("code",0);
        m.put("msg","");
        m.put("count",pageInfo.getTotal());
        m.put("data",pageInfo.getList());
        return m;
    }
    @ResponseBody
    @RequestMapping("upload")
    public Map upload(MultipartFile file) throws Exception {
        String s = UUID.randomUUID().toString()+file.getOriginalFilename();
        file.transferTo(new File("D:\\fuwuqi",s));
        Map m = new HashMap();
        m.put("code",0);
        m.put("pic",s);
        return m;
    }
    @ResponseBody
    @RequestMapping("add")
    public Map add(Banner banner){
        Map m = new HashMap();
        try {
            bannerService.insert(banner);
            m.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("flag",false);
        }
        return m;
    }
    @ResponseBody
    @RequestMapping("delete")
    public Map delete(Integer bannerId){
        Map m = new HashMap();
        try {
            bannerService.deleteByPrimaryKey(bannerId);
            m.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("flag",false);
        }
        return m;
    }
    @ResponseBody
    @RequestMapping("selectOne")
    public Banner selectOne(Integer bannerId){
        System.out.println("bannerId==="+bannerId);
        Banner banner = bannerService.selectByPrimaryKey(bannerId);
        System.out.println("banner==="+banner);
        return banner;
    }
    @ResponseBody
    @RequestMapping("update")
    public Map update(Banner banner){
        Map m = new HashMap();
        try {
            bannerService.updateByPrimaryKeySelective(banner);
            m.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("flag",false);
        }
        return m;
    }
}
